package nio;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * User: Administrator
 * Date: 2021/4/7
 * Time: 14:43
 * Description:
 */
public class FileNioFastCopy {
    public static void main(String[] args) {
        try (
                FileInputStream input = new FileInputStream("C:\\Users\\Administrator\\Desktop\\copy2.txt");
                FileOutputStream output = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\copy3.txt");
                FileChannel inputChannel = input.getChannel();
                FileChannel outputChannel = output.getChannel();
        ) {
            long size = inputChannel.size();
            long pos = 0;
            long count = 0;
            while (pos < size) {
                count = size - pos > 1024 ? 1024 : size - pos;
                pos += outputChannel.transferFrom(inputChannel, pos, count);
            }
            outputChannel.force(true);
        } catch (IOException ignored) {

        }
    }
}
